package com.elves.auth.controller;

import com.elves.auth.api.CommonResult;
import jakarta.annotation.Nullable;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.datetime.standard.DateTimeFormatterFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.config.annotation.web.oauth2.login.TokenEndpointDsl;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
//import org.springframework.security.oauth2.provider.endpoint.TokenEndpoint;
//import org.springframework.security.oauth2.core.OAuth2AccessToken;
//import org.springframework.security.oauth2.common.OAuth2AccessToken;
//import org.springframework.security.oauth2.provider.endpoint.TokenEndpoint;
//import org.springframework.security.oauth2.core.OAuth2AccessToken;
//import org.springframework.security.oauth2.provider.endpoint.TokenEndpoint;
import org.springframework.security.oauth2.core.OAuth2AccessToken;
import org.springframework.stereotype.Controller;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.*;

//import com.mysql.c

import lombok.Getter;

import java.security.Principal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Map;

import com.elves.auth.api.Oauth2TokenDto;

@Getter
@Slf4j
@Controller
//@RequestMapping("/server")
public class AppController {

    static LocalDateTime time = LocalDateTime.now();
    static final DateTimeFormatter df = new DateTimeFormatterFactory().createDateTimeFormatter();

    //认证后即可访问
    @GetMapping(value = "/")
    @ResponseBody
    public ResponseEntity<Object> home() {
        return new ResponseEntity<>(
                "Welcome to elves auth! 系统启动时间：" + time.format(df),
                HttpStatus.OK);
    }

    //认证后即可访问
    @RequestMapping("/mine")
    @ResponseBody
    public Authentication mine(@Nullable Principal principal) {
        // code +clientID+ APP私钥 请求换取token
        // code +clientID+ 用户私钥 请求换取token
        //返回code，token的json
        //后续给按钮请求hi，获取用户资源
        //后续给菜单，请求logout
        log.info("principal:{}", principal);
        return SecurityContextHolder.getContext().getAuthentication();
    }

    @GetMapping(value = "/admin/{id}")
    @ResponseBody    //    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public ResponseEntity<Object> adminID(@PathVariable String id) {
        String nowTime = LocalDateTime.now().format(new DateTimeFormatterFactory().createDateTimeFormatter());
        return new ResponseEntity<>("/admin/【" + id + "】 北京时间：" + nowTime, HttpStatus.OK);
    }

    @GetMapping(value = "/user/{id}")
    @ResponseBody
    public ResponseEntity<Object> userID(@PathVariable String id) {
        String nowTime = LocalDateTime.now().format(new DateTimeFormatterFactory().createDateTimeFormatter());
        return new ResponseEntity<>("/user/【" + id + "】 北京时间：" + nowTime, HttpStatus.OK);
    }

    @GetMapping(value = "/vip/{id}")
    @ResponseBody
    public ResponseEntity<Object> vipID(@PathVariable String id) {
        String nowTime = LocalDateTime.now().format(new DateTimeFormatterFactory().createDateTimeFormatter());
        return new ResponseEntity<>("/vip/【" + id + "】  北京时间：" + nowTime, HttpStatus.OK);
    }

    @GetMapping(value = "/time")
    @ResponseBody
    public ResponseEntity<Object> time() {
        return new ResponseEntity<>(
                "Welcome to elves time! 系统时间：" + LocalDateTime.now().format(df),
                HttpStatus.OK);
    }

//    @Autowired
//    public TokenEndpointDsl tokenEndpointDsl;
////    @Autowired
////    private TokenEndpointDsl tokenEndpoint;
//    /**
//     * Oauth2登录认证
//     */
//    @RequestMapping(value = "/oauth/token", method = RequestMethod.POST)
//    public CommonResult<Oauth2TokenDto> postAccessToken(Principal principal, @RequestParam Map<String, String> parameters) throws HttpRequestMethodNotSupportedException {
////        AppController tokenEndpoint = new AppController();
//        tokenEndpoint.getTokenEndpoint();
//        OAuth2AccessToken oAuth2AccessToken = (OAuth2AccessToken) tokenEndpoint.postAccessToken(principal, parameters).getBody();
//        Oauth2TokenDto oauth2TokenDto = Oauth2TokenDto.builder()
//                .token(oAuth2AccessToken.getTokenValue())
////                .refreshToken(oAuth2AccessToken.getRefreshToken().getValue())
////                .expiresIn(oAuth2AccessToken.getExpiresIn())
//                .tokenHead("Bearer ").build();
//        return CommonResult.success(oauth2TokenDto);
//    }

}
